package com.cpi.cornpp.salesforcast.converter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.salesforcast.dto.SalesForecastDTO;
import com.cpi.cornpp.salesforcast.exception.SalesForecastException;
import com.cpi.cornpp.salesforcast.view.SalesForecastView;
import com.cs.common.utils.NumberUtil;

/**
 * Sales Forecast Converter
 * @author Edgar Joao
 *
 */
public class SalesForecastConverter {
	/**
	 * Convert DTOs to Views
	 * @param dto
	 * @return
	 * @throws SalesForecastException
	 */
	public SalesForecastView converterSalesForecastDtoToView(SalesForecastDTO dto) throws SalesForecastException{
		SalesForecastView view = null;
		String month = null;
		Calendar cal_dummy = null;
		Calendar cal_actual = null;
		SimpleDateFormat sdf = null;
		String allowMod = "1";
		try{
			cal_dummy = Calendar.getInstance();
			cal_actual = Calendar.getInstance();
			cal_actual.set(Calendar.DATE, 1);
			sdf = new SimpleDateFormat("MMM");
			if(dto != null){
				view = new SalesForecastView();
				view.setCustomer(String.valueOf(dto.getCustomer()));
				view.setMaterial(String.valueOf(dto.getMaterial()));
				view.setFiscalYear(String.valueOf(dto.getFiscalYear()));
				view.setPeriod(String.valueOf(dto.getPeriod()));
				
				cal_dummy.set(Calendar.DATE, 1);
				cal_dummy.set(Calendar.MONTH, dto.getPeriod().intValue()-1);
				cal_dummy.set(Calendar.YEAR, dto.getFiscalYear().intValue());
				
				if(cal_dummy.before(cal_actual)){
					allowMod = "0";
				}
				
				month = sdf.format(cal_dummy.getTime());
				view.setMonth(month.toUpperCase());
				view.setContracted(NumberUtil.convertQuantity(dto.getContracted()));
				view.setNonContracted(String.valueOf(dto.getNonContracted()));
				view.setManual(String.valueOf(dto.getManual()));
				view.setAllowMod(allowMod);
			}
		}catch( Exception exception ){
			SalesForecastException salesForecastException=null;
			exception.printStackTrace(System.out);
			salesForecastException= new SalesForecastException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw salesForecastException;
		}
		return view;
	}
	/**
	 * Convert Sales Forecast Views to DTOs
	 * @param view
	 * @return
	 * @throws SalesForecastException
	 */
	public SalesForecastDTO convertSalesForecastViewToDTO(SalesForecastView view) throws SalesForecastException{
		SalesForecastDTO dto = null;
		try{
			if(view != null){
				dto = new SalesForecastDTO();
				dto.setCustomer(new Integer(view.getCustomer()));
				dto.setMaterial(view.getMaterial());
				dto.setFiscalYear(new Integer(view.getFiscalYear()));
				dto.setPeriod(view.getPeriod() != null ? new Integer(view.getPeriod()) : new Integer(0));
				dto.setContracted(view.getContracted() != null ? new Double(view.getContracted().replaceAll(",", StringUtils.EMPTY)) : new Double(0));
				dto.setNonContracted(view.getNonContracted() != null ? new Double(view.getNonContracted()) : new Double(0));
				dto.setManual(view.getManual() != null ? new Integer(view.getManual()) : new Integer(0));
			}			
		}catch( Exception exception ){
			SalesForecastException salesForecastException=null;
			exception.printStackTrace(System.out);
			salesForecastException= new SalesForecastException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw salesForecastException;
		}
		return dto;		
	}
	/**
	 * Convert List of Views to DTOs
	 * @param salesForecastViews
	 * @return
	 * @throws SalesForecastException
	 */
	public List convertViewsToDTOs(List salesForecastViews) throws SalesForecastException{
		Iterator iterator = null;
		SalesForecastDTO dto = null;
		SalesForecastView view = null;
		List salesForecastList = null;
		try{
			salesForecastList = new ArrayList(0);
			iterator = salesForecastViews.iterator();
			while(iterator.hasNext()){
				view = (SalesForecastView) iterator.next();
				dto = this.convertSalesForecastViewToDTO(view);
				salesForecastList.add(dto);
			}			
		}catch(SalesForecastException salesForecastException){
			throw salesForecastException;
		}catch( Exception exception ){
			SalesForecastException salesForecastException=null;
			exception.printStackTrace(System.out);
			salesForecastException= new SalesForecastException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw salesForecastException;
		}
		return salesForecastList;
	}
	/**
	 * Convert DTOs to Views List
	 * @param salesForecastDTOs
	 * @return
	 * @throws SalesForecastException
	 */
	public List convertDTOsToViews(List salesForecastDTOs) throws SalesForecastException{
		Iterator iterator = null;
		SalesForecastDTO dto = null;
		SalesForecastView view = null;
		List salesForecastList = null;
		try{
			salesForecastList = new ArrayList(0);
			iterator = salesForecastDTOs.iterator();
			while(iterator.hasNext()){
				dto = (SalesForecastDTO) iterator.next();
				view = this.converterSalesForecastDtoToView(dto);												
				salesForecastList.add(view);
			}			
		}catch(SalesForecastException salesForecastException){
			throw salesForecastException;
		}catch( Exception exception ){
			SalesForecastException salesForecastException=null;
			exception.printStackTrace(System.out);
			salesForecastException= new SalesForecastException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw salesForecastException;
		}
		return salesForecastList;
	}
}
